package org.fing.edu.uy.admin.managers;

import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.fing.edu.uy.admin.config.ConfigConstants;
import org.fing.edu.uy.admin.config.MgrAdminConfig;
import org.fing.edu.uy.esbadp.core.AdpFlowTree;
import org.fing.edu.uy.esbadp.core.constants.JMXServiceConstants;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.KeyValuePair;
import org.fing.edu.uy.esbadp.core.entity.MetaDataConfig;
import org.fing.edu.uy.esbadp.core.utils.JMXManager;

public class MgrServiceManager implements IMgrServiceManager {
	
	private String jmxServiceURL;
	private String mBeanName;
	
	public MgrServiceManager(){
		jmxServiceURL = MgrAdminConfig.getInstance().getMBeanProperty(ConfigConstants.JMXEsbAdpURL);
		mBeanName = JMXServiceConstants.JMXServicesAdminMBean;
	}
	
	@Override
	public DMRegisteredService getVirtualService(Long serviceId) throws Exception {
		Object[] arguments = {serviceId};
		String[] signature = {Long.class.getName()};
		// result to return	
		DMRegisteredService service = null;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getVirtualService", arguments, signature);
		if (result != null) {
			service = (DMRegisteredService) result;
		}			
		return service;		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DMRegisteredService> getEquivalentsServices(Long serviceId) throws Exception {
		Object[] arguments = {serviceId};
		String[] signature = {Long.class.getName()};
		// result to return	
		List<DMRegisteredService> regServices = null;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getEquivalentsServices", arguments, signature);
		if (result != null) {			
			regServices = (List<DMRegisteredService>)result;
		}				
		return regServices;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<DMRegisteredService> getAllVirtualServices() throws Exception {	
		Object[] arguments = {  };
		String[] signature = {  };
		// result to return	
		List<DMRegisteredService> regServices = null;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getAllVirtualServices", arguments, signature);
		if (result != null) {			
			regServices = (List<DMRegisteredService>) result;
		}
		return regServices;		
	}

	@Override
	public MetaDataConfig getVirtualServiceMetadata(Long serviceID)	throws Exception {
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		// result to return	
		MetaDataConfig meta = new MetaDataConfig();
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getVirtualServiceMetadata", arguments, signature);
		if (result != null) {			
			meta = (MetaDataConfig) result;
		}			
		return meta;	
	}
	
	@Override
	public int persistVirtualService(DMRegisteredService virtualService) throws Exception {		
		Object[] arguments = {virtualService};
		String[] signature = {DMRegisteredService.class.getName()};
		// result to return	
		int serviceID = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "persistVirtualService", arguments, signature);
		if (result != null) {			
			serviceID = (Integer) result;
		}			
		return serviceID;		
	}

	@Override
	public void persistVirtualServiceMetadata(MetaDataConfig meta, Long serviceID) throws Exception {
		Object[] arguments = {meta,serviceID};
		String[] signature = {MetaDataConfig.class.getName(), Long.class.getName()};
		JMXManager.callOperation(jmxServiceURL, mBeanName, "persistVirtualServiceMetadata", arguments, signature);		
	}

	@Override
	public void persistEquivVirtualServices(List<DMRegisteredService> equiv, Long serviceID) throws Exception {
		Object[] arguments = {equiv,serviceID};
		String[] signature = {List.class.getName(), Long.class.getName()};
		JMXManager.callOperation(jmxServiceURL, mBeanName, "persistEquivVirtualServices", arguments, signature);	
	}

	@Override
	public int deleteEquivRelation(Long serviceID) throws Exception {
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		// result to return	
		int cant = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "deleteEquivRelation", arguments, signature);
		if (result != null) {			
			cant = (Integer) result;
		}				
		return cant;
	}

	@Override
	public int deleteVirtualServiceMetadata(String metaKey, Long serviceID)	throws Exception {
		Object[] arguments = {metaKey,serviceID};
		String[] signature = {String.class.getName(), Long.class.getName()};
		// result to return	
		int cant = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "deleteVirtualServiceMetadata", arguments, signature);
		if (result != null) {			
			cant = (Integer) result;
		}
		return cant;
	}

	@Override
	public int deleteVirtualService(Long serviceID) throws Exception {
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		// result to return	
		int cant = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "deleteVirtualService", arguments, signature);
		if (result != null) {			
			cant = (Integer) result;
		}			
		return cant;	
	}

	@Override
	public int updateService(DMRegisteredService virtualService) throws Exception {
		Object[] arguments = {virtualService};
		String[] signature = {DMRegisteredService.class.getName()};
		// result to return	
		int cant = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "updateService", arguments, signature);
		if (result != null) {			
			cant = (Integer) result;
		}				
		return cant;
	}

	@Override
	public int updateMetadata(KeyValuePair meta, Long serviceID) throws Exception{
		Object[] arguments = {meta,serviceID};
		String[] signature = {KeyValuePair.class.getName(), Long.class.getName()};
		// result to return	
		int cant = -1;
		Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "updateMetadata", arguments, signature);
		if (result != null) {			
			cant = (Integer) result;
		}			
		return cant;
	}

	@Override
	public void addToCanonicalXLST(InputStream inputstream, Long serviceID) throws Exception {
		String fileText = IOUtils.toString(inputstream, "UTF-8");
		Object[] arguments = {fileText,serviceID} ;
		String[] signature = {String.class.getName(),Long.class.getName()};		
		JMXManager.callOperation(jmxServiceURL, mBeanName, "saveToCanonicalXSL", arguments, signature);		
	}

	@Override
	public void addFromCanonicalXLST(InputStream inputstream, Long serviceID) throws Exception {
		String fileText = IOUtils.toString(inputstream, "UTF-8");
		Object[] arguments = {fileText,serviceID} ;
		String[] signature = {String.class.getName(),Long.class.getName()};		
		JMXManager.callOperation(jmxServiceURL, mBeanName, "saveFromCanonicalXSL", arguments, signature);			
	}
	
	@Override
	public String getToCanonicalXLST(Long serviceID) throws Exception {
		Object[] arguments = {serviceID} ;
		String[] signature = {Long.class.getName()};
		String xlst = null;
		Object result =  JMXManager.callOperation(jmxServiceURL, mBeanName, "getToCanonicalXSL", arguments, signature);
		if (result != null) {			
			xlst = (String) result;
		} 			
		return xlst;	
	}

	@Override
	public String getFromCanonicalXLST(Long serviceID) throws Exception {
		Object[] arguments = {serviceID} ;
		String[] signature = {Long.class.getName()};
		String xlst = null;
		Object result =  JMXManager.callOperation(jmxServiceURL, mBeanName, "getFromCanonicalXSL", arguments, signature);
		if (result != null) {			
			xlst = (String) result;
		} 			
		return xlst;		
	}

	@Override
	public AdpFlowTree getFlowTreeForService(Long serviceID) throws Exception {
		String mBeanName = JMXServiceConstants.JMXAdmAdaptationMBean;
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		return (AdpFlowTree)JMXManager.callOperation(jmxServiceURL, mBeanName, "getAdpFlowForService", arguments, signature);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<AdpFlowTree> getHistoricFlowTreeForService(Long serviceID)throws Exception {
		String mBeanName = JMXServiceConstants.JMXAdmAdaptationMBean;
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		return ( List<AdpFlowTree>)JMXManager.callOperation(jmxServiceURL, mBeanName, "getHistoricFlowTreeForService", arguments, signature);
	}

	@Override
	public void clearHistoricFlowTreeForService(Long serviceID)	throws Exception {
		String mBeanName = JMXServiceConstants.JMXAdmAdaptationMBean;
		Object[] arguments = {serviceID};
		String[] signature = {Long.class.getName()};
		JMXManager.callOperation(jmxServiceURL, mBeanName, "clearHistoricFlowTreeForService", arguments, signature);
	}
	
}
